<?php

namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\AdSlots\CreateRequest;
use App\Http\Requests\Admin\AdSlots\UpdateRequest;
use App\Models\Admin\AdManage;
use App\Models\Admin\AdSite;
use App\Models\Admin\AdSlots;
use Illuminate\Http\Request;
use Style;

class AdSlotsController extends CommonController{
    /**
     * 显示位置
     * @return mixed
     */
    public function index(){
        $adSlots = AdSlots::orderBy('updated_at','desc')->paginate(20);
        return Style::view('admin.adsystem.ad_slots.index',compact('adSlots'));
    }

    /**
     * 添加位置
     * @return mixed
     */
    public function create(){
        return Style::view('admin.adsystem.ad_slots.create');
    }

    /**
     * 添加位置提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $input = $request->only(['ad_name','ad_sign','ad_number','width','height']);
        $result = AdSlots::create($input);
        if($result){
            flash('添加位置管理成功', 'success');
            return redirect('admin/ad_slots');
        }else{
            return back()->with('errors','位置管理添加失败，请稍后重试！');
        }
    }

    /**
     * 显示位置
     * @param $id
     */
    public function show($id,Request $request){
        $ad_id=$request->ad_id;
        if(!empty($ad_id)){//有广告id的时候使用当前广告的所属站点
            $myAd=AdManage::where('id',$ad_id)->first();
            $ad_site=$myAd->ad_site;//所属站点
        }else{//否则使用主站点
            $adSite=AdSite::where('site_sign','main')->first();
            $ad_site=!empty($adSite->id)?$adSite->id:'';//所属站点
        }
        $adSlots = AdSlots::where('id',$id)->first();
        $adManage=AdManage::where('ad_slots',$id)->where((function ($query) use ($ad_site) {
            if(!empty($ad_site)){//没有站点的时候显示所有站点的广告
                $query->where('ad_site',$ad_site);
            }
        }))->where('status',1)->orderBy('sort','asc')->get();
        return Style::view('admin.adsystem.ad_slots.show',compact('adSlots','adManage','ad_id'));
    }

    /**
     * 屏蔽广告
     * @return \Illuminate\Http\JsonResponse
     */
    public function shieldAd(Request $request){
        $adManage=AdManage::where('id',$request->id)->first();
        $adManage->status=2;
        $adManage->save();
        return response()->json(['status' => 200,'msg' => '操作成功！']);
    }

    /**
     * 设置排序
     * @return \Illuminate\Http\RedirectResponse
     */
    public function setAdSort(Request $request){
        if(is_array($request->sort_array)){
            $arrayId=array();
            foreach($request->sort_array as $k=>$v){
                $arrayId[]=$k;
            }
            $adManage=AdManage::whereIn('id',$arrayId)->get();
            foreach($adManage as $k=>$v){
                if(!empty($request->sort_array[$v->id])){
                    AdManage::where('id',$v->id)->update(['sort' =>$request->sort_array[$v->id]]);
                }
            }
        }
        return back()->with('info','保存成功！');
    }

    /**
     * 编辑位置
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $adSlots = AdSlots::find($id);
        return Style::view('admin.adsystem.ad_slots.edit',compact('adSlots'));
    }

    /**
     * 更新位置
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request, $id){
        $input = $request->only(['ad_name','ad_sign','ad_number','width','height']);
        $result = AdSlots::where('id',$id)->update($input);
        if($result){
            flash('更新位置管理成功', 'success');
            return redirect('admin/ad_slots');
        }else{
            return back()->with('errors','更新前和更新后数据相同，更新失败！！');
        }
    }

    /**
     * 删除位置
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = AdSlots::where('id',$id)->delete();
        if($result){
            return response()->json(['status' => 0,'msg' => '删除位置管理成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '删除位置管理失败，请稍后重试！']);
        }
    }
}
